<?php
namespace QnuiRings\Controller;
use Think\Controller;
// 软件名称：微折促销管理信息系统
// 代码日期：v3.0 2017-8-31
// 作者：杨道泉 
// 文件名：MPageController.class.php 
// 功能： 读取、创建手机页面

class PageComponentController extends QnuiBaseController {
	public function save(){
		$data = I("data");
		$ordinal = I("ordinal");
		$pageId = I("page_id");
		$compId = I("comp_id");
		
		$COMP = M("PageComponentsInpage");
		foreach($ordinal as $ord){
			$save = Array("ordinal"=>$ord["ordinal"]);
			if($ord["is_delete"]=="true"){
				$save["status"]="delete";
			}
			$COMP->where(Array("id"=>$ord["compId"],"sid"=>session("SHOP_ID")))
				 ->save($save);
		}
				 
		$COMPSET = M("PageComponentSettingsInpage");
		foreach($data as $key=>$value){
			if($value!="title"){
				$COMPSET->where(Array("sid"=>session("SHOP_ID"),
								   "page_id"=>$pageId,
								   // "component_inpage_id"=>$compId,
								   "field_name"=>$key,
								   "name"=>"value"))
					 ->save(Array("val"=>$value));
			}
		}
		$res = new \stdClass();
		$this->ajaxReturn(pre_resp($res));
		
	}
	public function delete(){
		$comp_id = I("comp_id");
		$page_id = I("page_id");
		
		$COMP_SETTING = M("PageComponentSettingsInpage");
		$COMP_SETTING->where(Array("component_inpage_id"=>$comp_id,"page_id"=>$page_id,"sid"=>session("SHOP_ID")))
					 ->delete();
		$COMP = M("PageComponentsInpage");
		$re = $COMP->where(Array("id"=>$comp_id,"page_id"=>$page_id,"sid"=>session("SHOP_ID")))
			 ->delete();
		$res = new \stdClass();
		$res->compid=$comp_id;
		$res->page_id=$page_id;
		$res->re=$re;
		$this->ajaxReturn(pre_resp($res));
	}
	public function add(){
		$comp_code = I("comp_code");
		$page_id = I("page_id");
		$ordinal = I("ordinal");
		$mix = I("mix");
		$page_id = (int)$page_id;
		
		$COMP_PUBLIC = M("PageComponentsPublic");
		$comp_pub = current($COMP_PUBLIC->where(Array("comp_code"=>$comp_code))
							    ->getField("`id`,`name`, `cat`, `comp_code`, `ver_code`, `price`, `max_count`, `status`"));
		if($comp_pub===null){
			$res = new \stdClass();
			$res->code=910;
			$res->msg="抱歉，数据库中没有这个插件！";
			$this->ajaxReturn(pre_resp($res));
			die();
		}
		$CMPS = M("PageComponentsInpage");
		$comp_data = Array();
		$comp_data["sid"] = session("SHOP_ID");
		$comp_data["name"] = $comp_pub["name"];
		$comp_data["mix"] = $mix;
		$comp_data["ordinal"] = $ordinal;
		$comp_data["code"] = $comp_pub["comp_code"];
		$comp_data["model_private"] = false;
		$comp_data["status"] = $comp_pub["status"];
		$comp_data["cat"] = $comp_pub["cat"];
		$comp_data["page_id"] = $page_id;
		$component_inpage_id = $CMPS->add($comp_data);
		// INSERT INTO rings_page_component_settings_inpage (sid,page_id,component_inpage_id,field_name,name,val)
		// SELECT '64665313','12','19',CONCAT(field_name,'_UTSU4T'),name,if(name="name", CONCAT(val,'_UTSU4T'),val) 
		// FROM rings_page_component_settings_public 
		// WHERE component_id= '2'
		$CMPS->execute("INSERT INTO __PAGE_COMPONENT_SETTINGS_INPAGE__ (sid,page_id,component_inpage_id,field_name,name,val)
						SELECT '".session("SHOP_ID")."','".$page_id."','".$component_inpage_id."',CONCAT(field_name,'".$mix."'),name,if(name='name', CONCAT(val,'".$mix."'),val) 
						FROM __PAGE_COMPONENT_SETTINGS_PUBLIC__ 
						WHERE component_id= '".$comp_pub["id"]."'");
					
		$res = new \stdClass();
		$res->comp_id=$component_inpage_id;
		$res->comp_pub=$comp_pub;
		//$res->_sql=$CMPS->_sql();
		$this->ajaxReturn(pre_resp($res));		
	}
}